
//Vue全局方法
//import Vue from "vue/types/index";

exports.install = function(Vue, option){
  //是否登录
  Vue.prototype.isLogin = function(){
    let storageUser = sessionStorage.getItem("userSession");
    storageUser = storageUser ? JSON.parse(storageUser) : null;
    // return true;
    if( storageUser ){
      return storageUser
    }else {
      return false
    }
  };

  //验证信息
  Vue.prototype.verifyForm = {
    //手机号是否正确
    iphone(tel){
      if( tel ){
        let iphoneReg = /^1(3|4|5|7|8)\d{9}$/;
        if( tel.length > 0 && !iphoneReg.test(tel) ){
          return true
        }else{
          return false
        }
      }
    },

    //是否为手机或座机
    isIphoneOrTell(tel){ //11位
      if( tel ){
        let tellReg = /^[0-9]{11}$/;
        if( tel.length > 0 && !tellReg.test(tel) ){
          return true
        }else{
          return false
        }
      }
    },

    //座机
    isTell(tel){ //8位或11位
      if( tel ){
        let tellReg = /^\d{8}$|^\d{11}$/;
        if( tel.length > 0 && !tellReg.test(tel)  ){
          return true
        }else{
          return false
        }
      }
    },

    //是否字符和数字
    letterAndNumber(val){
      if( val ){
        let reg = /^[0-9a-zA-Z]*$/g;
        if( val.length > 0 && !reg.test(val) ){
          return true
        }else{
          return false
        }
      }
    },

    //姓名
    isName(val){
      if( val ){
        let reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
        if( val.length > 0 && !reg.test(val) ){
          return true
        }else{
          return false
        }
      }
    },
    //是否为整数
    isNumber(val){
      if( val ){
        let reg = /^[0-9]*$/;
        if( val.length > 0 && !reg.test(val) ){
          return true
        }else{
          return false
        }
      }
    },

    //车排号验证
    isCarCode(val){
      if( val ){
        let reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
        if( val.length > 0 && !reg.test(val) ){
          return true
        }else{
          return false
        }
      }
    },

    //身份证
    idCode(idCode){
      if( idCode ){
        let idCodeReg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
        if( idCode.length > 0 && !idCodeReg.test(idCode) ){
          return true
        }else{
          return false
        }
      }
    },

    //是否为空验证
    isNull(val){
      //val = Vue.prototype.JPtool.subBlank(val);
      if( val === "" || val === "-1" || val === null || val === "null" ){
        return true
      }
      return false;
    },


    //验证经度
    ifLongitude(val){
      if( val && val.length > 0 ){
        let longrg = /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,8})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,8}|180)$/;
        if(!longrg.test(val)){
          return true;
        }
        return false;
      }
    },

    //验证纬度
    ifLatitude(val){
      if( val && val.length > 0 ){
        let longrg = /^(\-|\+)?([0-8]?\d{1}\.\d{0,8}|90\.0{0,8}|[0-8]?\d{1}|90)$/;
        if(!longrg.test(val)){
          return true;
        }
        return false;
      }
    },

    //验证文件大小是否超标
    fileSize(val){
      let value = (val/1024/1024).toFixed(2);
      if( value > 50 ){
        return true;
      }else{
        return false;
      }
    },

    //验证IP地址
    isIP(val){
      if( val && val.length > 0 ){
        let ipgReg =  /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
        if(!ipgReg.test(val)){
          return true;
        }
        return false;
      }
    },

    //验证是否为音频
    isAudio(type){
      if(type === "mp3" || type === "mp4" || type === "wma" ||
        type === "ape" || type === "mpc" || type === "gss" ||
        type === "wave" || type === "cd" || type === "flac" ||
        type === "tta" || type === "aiff" ||
        type === "au"){
        return true
      }
      return false;
    },

    //验证是否为音频
    isVideo(type){
      if( type === "avi" || type === "wmv" || type === "rmvb" ||
        type === "mpeg1" || type === "mpeg2" || type === "mpeg4" ||
        type === "map4" || type === "3gp" || type === "asf" ||
        type === "swf" || type === "vob" || type === "dat" ||
        type === "mov" || type === "m4v" || type === "flv" ||
        type === "f4v" || type === "mkv" || type === "mts" ||
        type === "ts" || type === "ogg"){
        return true
      }
      return false;
    },

    //验证是否为文档文件
    isDomFile(type){
      if( type === "doc" ||  type === "docx" || type === "txt" ||
         type=== "xlsx" || type === "xls" || type === "pdf"){
        return true
      }
      return false;
    },

    //验证是否为文档文件
    isXls(type){
      if(type=== "xlsx" || type === "xls"){
        return true
      }
      return false;
    },

    //验证是否为图片
    isImg(type){
      if(type === "jpg" || type === "png" || type === "jpeg" ||
        type === "gif" ){
        return true
      }
      return false;
    },

  };

  //工具函数
  Vue.prototype.JPtool = {
    //根据部门id查部门名称 返回部门名称， 参数为部门id 不调用部门接口
    queryStationName(itemStationId,stationAll){
      let stationName = "";
      for( let i=0;i<stationAll.length;i++ ){
        if( stationAll[i].id === itemStationId ){
          stationName = stationAll[i].name;
        }
      }
      return stationName;
    },

    //去除前后空格
    subBlank(val){
      val = val.toString();
      let newVal = val.replace(/^\s+|\s+$/g,"");
      return newVal;
    },

    /**
     *
     * 时间戳转换转年月日时分秒
     * 参数一 时间戳
     * 参数二 date: 年月日  dateAll: 年月日时分秒
     *
     * */
    timestampChangeDate(value,dateType){
      let time = new Date(value);
      let y = time.getFullYear();
      let m = time.getMonth()+1;
      let d = time.getDate();
      let h = time.getHours();
      let mm = time.getMinutes();
      let s = time.getSeconds();
      function add0(m){return m<10?'0'+m:m }
      return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
    },

    /**
     *
     * 正序排序
     * 参数一 需要排序的数据 参数二 根据参数一个字段排序
     * */
    ascendingSort(arr,type){
      return arr.sort(function(a,b){ return a[type] - b[type]})
    },

    /**
     *
     * 根据数据生成一个搜索使用的新对象
     *
     *  dftStr : 下拉框的一条数据名称 值默认空
     *  key : 传递进来需要转译的key
     *  value：传递进来需要转译的key
     *
     *  原[ {'a':'','b':''} ]返回对象格式 [ {'key':'','value':''} ]
     *
     * */
    searchSelectPublicObj(dftStr,key,value,data){
      let searchObjSelectArr = [{"key":dftStr,"value":""}];
      for( let i=0;i<data.length;i++ ){
        searchObjSelectArr.push(
          {"key":data[i][key],"value":data[i][value]}
        )
      }
      return searchObjSelectArr
    },
    phoneVerifyFn  (val) {
      let reg = /^[0-9]*$/
      if (reg.test(val)) {
        return val
      } else {
        if(val){
          return val.replace(/[^\d]+/g, "")
        }
      }
    },
    numberVerifyFn (val) {
      if(val) {
        return val.replace(/[^\d]+/g, "")
      }
    },
    // 验证经度
    longitudeVerifyFn (val) {
      if(val.toString().length > 4) {

      } else {
        if (val) {
          return val.replace(/^(\-|\+)?[^\d{0,3}\.]+/g, "")
        }
      }
      let longrg = /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,8})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,8}|180)$/;
    }
  };

  // 十六进制颜色值转为rgb颜色数组
  Vue.prototype.colorRgb = function(sColor){
    //十六进制颜色值的正则表达式
    var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    sColor = sColor.toLowerCase();
    if(sColor && reg.test(sColor)){
      if(sColor.length === 4){
        var sColorNew = "#";
        for(var i=1; i<4; i+=1){
          sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));
        }
        sColor = sColorNew;
      }
      //处理六位的颜色值
      var sColorChange = [];
      for(var i=1; i<7; i+=2){
        sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));
      }
      return sColorChange;
    }else{
      return sColor;
    }
  };

  // 部门和警员树中在部门后面增加警员人数
  // 例子：xxx部门(6)
  Vue.prototype.addPeopelNumAfterStationName = function(data){
    let peopleAry = [];
    data.forEach(function(item){
      if(item.hasOwnProperty('policeCode')) {
        peopleAry.push(item);
      }
    });
    for (let i = 0; i < data.length; i++ ){
      let num = 0;
      for(let j = 0; j < peopleAry.length; j++){
        if(data[i].id == peopleAry[j].pId){
          num += 1;
        }
      }
      if(num > 0){
        data[i].name =  data[i].name + "("+num+")";
        // //console.log(data[i].name);
      }
    }
    return data;
  };

  // 解析document.cookie属性值
    Vue.prototype.getcookie = function() {
      var cookie = {};
      var all = document.cookie;
      if (all === "")
        return cookie;
      var list = all.split("; ");
      for(var i = 0; i < list.length; i++) {
        var cook = list[i];
        var p = cook.indexOf("=");
        var name = cook.substring(0,p);
        var value = cook.substring(p+1);
        value = decodeURIComponent(value);
        cookie[name] = value;
      }
      return cookie;
  };

   Vue.prototype.s_logwrite = function(module,log){
    //console.log(module);
    //console.log(log);
    this.$axios.post (this.HOST+ '/log/addlogV2',{
      log: log,
      moduleName:module,
      operator:this.isLogin().userId,
      accountName:this.isLogin().loginName,
    })
      .then(res => {
        //console.log(res);
      }).catch( err => {
      //console.log(err);
    })
  };
  Vue.prototype.s_alert = function(txt,time) {
    time = time ? time : 1700;
    this.$store.commit('setMessageDialogObj',{
      visible: true,
      time:time,
      txt: txt
    })
  };

  Vue.prototype.s_loading = function (boolean) {
    this.$store.commit('setLoading', boolean)
  }

};
